import csv
import time
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Edge()
driver.get("https://www.zhipin.com/")
driver.find_element(By.CSS_SELECTOR, "#wrap > div.column-search-panel.search-panel-new > div > div.search-box > div.search-form > form > div.search-form-con > p > input").send_keys("大数据开发工程师")
driver.find_element(By.CSS_SELECTOR, "#wrap > div.column-search-panel.search-panel-new > div > div.search-box > div.search-form > form > button").click()
time.sleep(10)

mydictheader = ["工作岗位", "工作地址", "工作薪资", "工作要求", "招聘人员", "公司名字", "公司信息", "工作对象", "工作福利"]
with open("D:/JetBrains Projects/Pycharm Projects/python-learn/爬虫作业/boss直聘/boss直聘.csv", "a", encoding="UTF-8", newline="") as f:
    dictwriter = csv.DictWriter(f, mydictheader)
    dictwriter.writeheader()
    for i in range(2):
        mylist = driver.find_elements(By.CSS_SELECTOR, ".search-job-result ul li")
        for li in mylist:
            try:
                mydict = {
                    "工作岗位": li.find_element(By.CSS_SELECTOR, ".job-title.clearfix .job-name").text,
                    "工作地址": li.find_element(By.CSS_SELECTOR, ".job-title.clearfix .job-area").text,
                    "工作薪资": li.find_element(By.CSS_SELECTOR, ".job-info.clearfix .salary").text,
                    "工作要求": li.find_element(By.CSS_SELECTOR, ".job-info.clearfix .tag-list").text.replace("\n", "，"),
                    "招聘人员": li.find_element(By.CSS_SELECTOR, ".job-info.clearfix .info-public").text,
                    "公司名字": li.find_element(By.CSS_SELECTOR, ".job-card-right .company-name").text,
                    "公司信息": li.find_element(By.CSS_SELECTOR, ".job-card-right ul").text,
                    "工作对象": li.find_element(By.CSS_SELECTOR, ".job-card-footer.clearfix ul").text,
                    "工作福利": li.find_element(By.CSS_SELECTOR, ".job-card-footer.clearfix .info-desc").text
                }
                dictwriter.writerow(mydict)
            except:
                pass
        driver.find_element(By.CSS_SELECTOR, "#wrap > div.page-job-wrapper > div.page-job-inner > div > div.job-list-wrapper > div.search-job-result > div > div > div > a:nth-child(10)").click()
        time.sleep(10)
